/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.knowledge.doc.quartz.task;

import com.wallellen.core.sql.query.DBRule;
import com.wallellen.core.sql.query.DataQuery;
import com.wallellen.core.sql.result.DataResult;
import com.wallellen.knowledge.doc.server.DocRunInfoInter;
import com.wallellen.util.spring.BeanFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.sql.SQLException;
import java.util.Map;

/**
 * 计算文档热度的定时器
 *
 * @author <a href="mailto:wallellen@hotmail.com">WALLE</a>
 */
public class RecountDocHotnumTask implements Job {
    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        DocRunInfoInter docRunInfoIMP = (DocRunInfoInter) BeanFactory
                .getBean("farmDocRunInfoImpl");
        DataResult result;
        DataQuery query;
        query = DataQuery.getInstance("1", "id,title", "farm_doc");
        query.setCurrentPage(1);
        query.addRule(new DBRule("STATE", "1", "="));
        query.addRule(new DBRule("READPOP", "1", "="));
        int i = 1;
        int docnum = 0;
        while (true) {
            try {
                query.setPagesize(100);
                query.setCurrentPage(i++);
                result = query.search();
                if (result.getResultList().size() <= 0) {
                    break;
                }
                for (Map<String, Object> node : result.getResultList()) {
                    String docid = node.get("ID").toString();
                    docRunInfoIMP.reCountDocHotNum(docid);
                    docnum++;
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
                break;
            }
        }
        System.out.println("对文档(" + docnum + "条数据)计算热度");
    }
}
